/**
 * 
 */
package com.ruyipu.forum.adapter;

import java.util.List;

import org.apache.http.protocol.HTTP;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruyipu.forum.R;
import com.ruyipu.forum.base.ImageService;
import com.ruyipu.forum.base.ImageService.ImageCallback;
import com.ruyipu.forum.interfaces.IItem;
import com.ruyipu.forum.item.Comment;
import com.ruyipu.forum.item.Discussion;

/**
 * @author donald
 * 
 */
public class CommentAdapter extends BaseAdapter {

	/**
	 * 
	 * @param context
	 * @param dataList
	 */
	public CommentAdapter(Context context, List<IItem> dataList) {
		super(context, dataList);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		Discussion discussion = null;
		try {
			discussion = (Discussion) dataList.get(position);
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (null != discussion) {
			final LinearLayout layout = (LinearLayout) getView(null,
					R.layout.item_discussion);
			ImageView ivGravatar = (ImageView) layout
					.findViewById(R.id.ivItemDiscussionGravatar);
			String imageUrl = discussion.getFirstPhoto();
			ivGravatar.setTag(imageUrl);
			ivGravatar.setImageBitmap(ImageService.loadImage(imageUrl,
					new ImageCallback() {

						public void imageLoaded(Bitmap imageBitmap,
								String imageUrl) {
							ImageView imageView = (ImageView) layout
									.findViewWithTag(imageUrl);
							imageView.setImageBitmap(imageBitmap);
						}
					}));
			TextView tvAuthor = (TextView) layout
					.findViewById(R.id.tvItemDiscussionAuthor);
			TextView tvPublishedDate = (TextView) layout
					.findViewById(R.id.tvItemDiscussionPublishedDate);
			TextView tvIp = (TextView) layout
					.findViewById(R.id.tvItemDiscussionIP);
			TextView tvCountComments = (TextView) layout
					.findViewById(R.id.tvItemDiscussionCommentCount);
			WebView wbContent = (WebView) layout
					.findViewById(R.id.wbItemDiscussionContent);

			tvAuthor.setText(discussion.getFirstName());
			tvPublishedDate.setText(discussion.getDate());
			tvIp.setText(discussion.getIP());
			tvCountComments.setText(String.format(
					context.getString(R.string.comment_count),
					discussion.getCountComment()));

			wbContent.loadDataWithBaseURL(null, discussion.getContent(),
					"text/html", "UTF-8", null);
			return layout;
		} else {
			Comment comment = null;
			try {
				comment = (Comment) dataList.get(position);
			} catch (Exception e) {
				return null;
			}
			final LinearLayout layout = (LinearLayout) getView(null,
					R.layout.comment_item);
			ImageView ivInsertPhoto = (ImageView) layout
					.findViewById(R.id.ivCommentItemInsertPhoto);
			String imageUrl = comment.getInsertPhoto();
			ivInsertPhoto.setTag(imageUrl);
			ivInsertPhoto.setImageBitmap(ImageService.loadImage(imageUrl,
					new ImageCallback() {

						public void imageLoaded(Bitmap imageBitmap,
								String imageUrl) {
							ImageView imageView = (ImageView) layout
									.findViewWithTag(imageUrl);
							if (null != imageBitmap && null != imageView) {
								imageView.setImageBitmap(imageBitmap);
							}
						}
					}));
			TextView tvInsertName = (TextView) layout
					.findViewById(R.id.tvCommentInsertName);
			TextView tvCommentIP = (TextView) layout
					.findViewById(R.id.tvCommentIP);
			WebView wvCommentBody = (WebView) layout
					.findViewById(R.id.wvCommentBody);

			tvInsertName.setText(comment.getInsertName());
			tvCommentIP.setText(comment.getIP());
			wvCommentBody.loadDataWithBaseURL(null, comment.getBody(),
					"text/html", HTTP.UTF_8, null);
			return layout;
		}
	}

}
